package com.ctshk.rpc.custom.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.custom.dto.inquiry.InquiryInfoDTO;
import com.ctshk.rpc.custom.entity.CustomizedInquiry;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.custom.req.inquiry.InquiryListSqlReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;

/**
 * <p>
 * 询价单 Mapper 接口
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-13
 */
public interface CustomizedInquiryMapper extends BaseMapper<CustomizedInquiry> {

    /**
     * 通过询价单号获取询价详情
     * @param inquiryNumber
     * @return
     */
    @Select("SELECT id,inquiry_title,destination,departure_place,departure_date,travel_days,night_days,total_people_num,capita_budget,op_number,op_name,inquiry_end_time,salesperson_number,salesperson_name,service_require_files,customer_list_files,customer_type,big_client_name,contact_cn_surname,contact_cn_name,contact_telephones,contact_email,internal_remark,inquiry_status,refuse_remark,refuse_time,gmt_create \n" +
            "FROM t_customized_inquiry WHERE inquiry_number = #{inquiryNumber} and is_deleted = 0 LIMIT 1")
    InquiryInfoDTO queryByNumber(@Param("inquiryNumber")String inquiryNumber);

    /**
     * 根据报价状态获取询价列表
     */
    @Select("<script>"+"SELECT a.* FROM t_customized_inquiry a LEFT JOIN t_customized_quotation b ON a.id = b.inquiry_id\n" +
            "WHERE a.is_deleted = 0 <if test=\"req.inquiryNumber != null and req.inquiryNumber != ''\">AND a.inquiry_number LIKE CONCAT('%',#{req.inquiryNumber},'%')</if> \n" +
            "<if test=\"req.quotationStatus != null\">AND b.quotation_status = #{req.quotationStatus}</if> \n" +
            "<if test=\"req.inquiryStatus != null and req.inquiryStatus != 0\">AND a.inquiry_status = #{req.inquiryStatus}</if> \n" +
            "<if test=\"req.queryType == 1 and req.opId != null\">AND a.inquiry_status != 1 AND a.op_id = #{req.opId}</if> \n" +
            "<if test=\"req.queryType == 2 and req.opId != null\">AND a.create_id = #{req.opId}</if> \n" +
            "<if test=\"req.inquiryTitle != null and req.inquiryTitle != ''\">AND a.inquiry_title LIKE CONCAT('%',#{req.inquiryTitle},'%')</if> \n" +
            "<if test=\"req.bigClientId != null and req.bigClientId != 0\">AND a.big_client_id = #{req.bigClientId}</if> \n" +
            "<if test=\"req.salespersonId != null and req.salespersonId != 0\">AND a.salesperson_id = #{req.salespersonId}</if> \n" +
            "<if test=\"req.departurePlace != null and req.departurePlace != ''\">AND a.departure_place LIKE CONCAT('%',#{req.departurePlace},'%')</if> \n" +
            "<if test=\"req.destination != null and req.destination != ''\">AND a.destination LIKE CONCAT('%',#{req.destination},'%')</if> \n" +
            "<if test=\"req.travelDays != null and req.type == 1\">AND a.travel_days = #{req.travelDays}</if> \n" +
            "<if test=\"req.travelDays != null and req.type == 2\">AND FIND_IN_SET(travel_days,#{req.travelDays})</if> \n" +
            "<if test=\"req.travelDays != null and req.type == 3\">AND a.travel_days > #{req.travelDays}</if> \n" +
            "<if test=\"req.permision and req.permisionList != null\">AND a.create_id IN " +
                "<foreach collection=\"req.permisionList\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
                "#{id}\n" +
                "</foreach>"+
            "</if> \n" +
            "<if test=\"req.beginDate != null and req.endDate != null\">AND a.departure_date between #{req.beginDate} and #{req.endDate}</if> \n" +
            "<if test=\"req.beginDate != null and req.endDate == null\">AND a.departure_date between #{req.beginDate} and NOW()</if> \n" +
            "ORDER BY a.gmt_create DESC"+"</script>")
    IPage<CustomizedInquiry> queryListByQuatationStatus(@Param("req") InquiryListSqlReq sqlReq, IPage<CustomizedInquiry> inquiryIPage);

    /**
     * 手机短信查询
     * @param inquiryNumber
     * @param queryNumber
     * @return
     */
    @Select("SELECT id\n" +
            "FROM t_customized_inquiry\n" +
            "WHERE is_deleted = 0 AND inquiry_number = #{inquiry_number} AND query_number = #{query_number} ORDER BY gmt_create DESC LIMIT 1")
    Long query(@Param("inquiry_number") String inquiryNumber, @Param("query_number") String queryNumber);
}
